#!/usr/bin/php
<?php
/**
 * <https://y.st./>
 * Copyright © 2015-2017 Alex Yst <mailto:copyright@y.st>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org./licenses/>.
**/

/**
 * This source code requires PHP to compile. It was tested with PHP
 * 5.6.6-2, but will likely work with anything at least as recent as
 * PHP 5.3. Any PHP version older than that lacks name spaces, and will
 * not work with this code. To install PHP without installing a Web
 * server, install the "php5-cli" on Debian. The package may be under a
 * different name on other systems and distributions. Additionally, you
 * will need PECL's gnupg_*() functions.
 * 
 * sudo aptitude install php5-cli php5-gnupg
 * 
 * You will need to install the include.d PHP library, which is not
 * available from the Debian repository. It can be found at
 * <https://notabug.org./y.st./include.d>.
**/

// Used for end-of-run statistics output
$time_script_started = time();

// A little setup ...
spl_autoload_register();
require 'st/y/function/error_handler.php';
require 'st/y/function/highlight_string.php';
require 'st/y/function/recurse_dir.php';
require 'st/y/function/update_file.php';
set_error_handler('\\st\\y\\error_handler');
require __DIR__.'/source/variables.php';
require __DIR__.'/source/closure.php';

// If invalid arguments are used, the whole compilation process should
// halt and an error message should be presented.
if(isset($invalid_arguments)):
	die("\nInvalid arguments: ".implode(' ', $invalid_arguments)."\n\n");
endif;

// If the canary chirps ...
if(in_array('--canary', $argv)):
	ob_start();
	require __DIR__.'/source/canary.php';
	$canary = $pgp->sign(ob_get_clean());
	\st\y\update_file(__DIR__.'/static/a/canary.txt', $canary);
else:
	echo "\nYou did not run this script using the \"--canary\" flag. Is everything okay?\n\n";
endif;

// Tell PHP to use detached signatures, so the XHTML pages will still validate.
$pgp->setsignmode(GNUPG_SIG_MODE_DETACH);

// Some files are static, and only need to be copied, not compiled.
foreach(\st\y\recurse_dir(__DIR__.'/static') as $filename):
	$dirname = dirname(__DIR__."/compiled/$filename");
	if(!is_dir($dirname)):
		mkdir($dirname, 0777, true);
	endif;
	\st\y\update_file(__DIR__."/compiled/$filename", file_get_contents(__DIR__."/static/$filename"));
	$should_exist[$filename] = true;
endforeach;

// Other files are compiled.
foreach(\st\y\recurse_dir(__DIR__."/source/pages") as $filename):
	require __DIR__."/source/pages/$filename";
	$¢_build_page($filename, $xhtml);
endforeach;

// Account for moved pages.
require __DIR__.'/source/redirect.php';

// Directory indexes should be built.
require __DIR__.'/source/weblog_index.php';

// Copy the English index to the main index because the plan to set up
// an Esperanto version of the website sort of flopped.
copy(__DIR__.'/compiled/en/index.xhtml', __DIR__.'/compiled/index.xhtml');
$should_exist['index.xhtml'] = true;

foreach(\st\y\recurse_dir(__DIR__.'/compiled') as $filename):
	if(!isset($should_exist[$filename])):
		unlink(__DIR__."/compiled/$filename");
	endif;
endforeach;

// End-of-run statistics:
$time_until_script_ended = time() - $time_script_started;
echo "The script completed in $time_until_script_ended seconds.\n";
